package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Login;
import com.example.demo.entity.User;
import com.example.demo.mapper.LoginMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginMapper loginMapper;

    @RequestMapping("/page/{page}/{size}")
    @ResponseBody
    public Page<User> page(@PathVariable("page") Integer page,
                           @PathVariable("size") Integer size) {
        Page<User> page1 = new Page<>(page, size);
        Page<User> page2 = userMapper.selectPage(page1, null);
        return page2;
    }

    @ResponseBody
    @RequestMapping("/getUser/{userId}")
    public List<User> getUser(@PathVariable("userId") Integer userId) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserId,userId);
        List<User> users = userMapper.selectList(lambdaQueryWrapper);
        return users;
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public int delete(@PathVariable("id") Integer id) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserId, id);
        int delete = userMapper.delete(lambdaQueryWrapper);
        return delete;
    }

    @GetMapping("/insert")
    @ResponseBody
    public int insert(User user){
        int insert = userMapper.insert(user);
        return insert;
    }

    @GetMapping("/update/{userId}")
    @ResponseBody
    public int update(User user,
                      @PathVariable("userId") Integer userId){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserId,userId);
        int update = userMapper.update(user, lambdaQueryWrapper);
        return update;
    }

}
